package com.c201801020208.ybl.model.impl;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;


import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.c201801020208.ybl.R;
import com.c201801020208.ybl.model.impl.ChatActivity;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.qcloud.tim.uikit.component.TitleBarLayout;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.ConversationListLayout;
import com.tencent.qcloud.tim.uikit.modules.conversation.base.ConversationInfo;


public class ChatFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_chat, container, false);

    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 从布局文件中获取会话列表面板
        ConversationLayout conversationLayout = getActivity().findViewById(R.id.conversation_layout);
        // 初始化聊天面板
        conversationLayout.initDefault();
        ConversationListLayout listLayout = conversationLayout.getConversationList();
        listLayout.setItemTopTextSize(20); // 设置 item 中 top 文字大小
        listLayout.setItemBottomTextSize(14);// 设置 item 中 bottom 文字大小
        listLayout.setItemDateTextSize(12);// 设置 item 中 timeline 文字大小
        listLayout.setItemAvatarRadius(10); // 设置 adapter item 头像圆角大小
        listLayout.disableItemUnreadDot(false);// 设置 item 是否不显示未读红点，默认显示
        listLayout.setPaddingRelative(10, 20, 10, 20);

        // 获取 TitleBarLayout
        TitleBarLayout titleBarLayout = conversationLayout.findViewById(R.id.conversation_title);
        // 设置标题
        titleBarLayout.setTitle("", TitleBarLayout.POSITION.MIDDLE);
        // 隐藏左侧 Group
        titleBarLayout.getLeftGroup().setVisibility(View.GONE);
        // 设置右侧的菜单图标
        titleBarLayout.setRightIcon(R.drawable.conversation_more);
        titleBarLayout.getRightGroup().setVisibility(View.GONE);
        titleBarLayout.setBackgroundResource(R.drawable.boss);


        //点击聊天
        listLayout.setOnItemClickListener(new ConversationListLayout.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, ConversationInfo messageInfo) {
                ChatInfo chatInfo = new ChatInfo();
                chatInfo.setType(messageInfo.isGroup() ? V2TIMConversation.V2TIM_GROUP : V2TIMConversation.V2TIM_C2C);
                chatInfo.setId(messageInfo.getId());
                chatInfo.setChatName(messageInfo.getTitle());
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                intent.putExtra("CHAT_INFO", chatInfo);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
}
